package com.fare.taluo.core.domain.config.city.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.Order;
import org.springframework.stereotype.Repository;

import com.dayatang.domain.QuerySettings;
import com.dayatang.hibernate.EntityRepositoryHibernate;
import com.fare.taluo.core.domain.config.city.PortEntity;
import com.fare.taluo.core.domain.config.city.PortEntityRepository;
import com.fare.taluo.util.StringUtils;

@Repository
public class PortEntityRepositoryImpl extends EntityRepositoryHibernate
		implements PortEntityRepository {
	
	@SuppressWarnings("unchecked")
	@Override
	public List<PortEntity> findAll() {
		return getSession().createCriteria(PortEntity.class)
				.addOrder(Order.asc("priority")).addOrder(Order.asc("id")).setCacheable(true).list();
	}
    
	@Override
	public PortEntity findByPortCode(String portCode) {
		return super.getSingleResult(QuerySettings.create(PortEntity.class).eq("code", portCode));
	}
	@Override
	public List<PortEntity> findByCityCode(String cityCode) {
		String hql = "from PortEntity t where t.city.code = ?";
		List<Object> list = new ArrayList<Object>();
		list.add(cityCode);
		return super.find(hql, list.toArray(), PortEntity.class);
	}
    
    @Override
	public List<PortEntity> findPortEntityByCityName(String cityName) {
    	String hql = "from PortEntity t";
		String condition = "";
		List<Object> list = new ArrayList<Object>();
		if(StringUtils.isNotEmpty(cityName)) {
			condition = " where t.city.name like ?";
			list.add("%"+cityName+"%");
		}
		hql = hql + condition;
		return super.find(hql, list.toArray(), PortEntity.class);
	}
    @Override
	public List<PortEntity> findPortByPortName(String portName){
		String hql = "from PortEntity t";
		String condition = "";
		List<Object> list = new ArrayList<Object>();
		if(StringUtils.isNotEmpty(portName)) {
			condition = " where t.name like ?";
			list.add( '%' + portName + '%');
		}
		hql = hql + condition;
		return super.find(hql, list.toArray(), PortEntity.class);
	}


}
